/**
* @authors Duong NGUYEN <darl2ng@gmail.com>, Anh NGO <ngocanh.ngo@gmail.com>
* @version 0.9
* Copyright 2010
* 
* The MIT License
* Copyright (c) 2010 <Duong NGUYEN <darl2ng@gmail.com>, Anh NGO <ngocanh.ngo@gmail.com>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package  {
	
	import com.ng.ResHelp.c_ResLoader;
	import flash.utils.getDefinitionByName;

	public class c_ResLoader2 extends c_ResLoader	{
		
		static private var __OverrideInstance:c_ResLoader2;
		
		static public function GetInstance():c_ResLoader2 {
			if(__OverrideInstance == null)
				__OverrideInstance = new c_ResLoader2();
			return __OverrideInstance;
		}				
		public function c_ResLoader2(){
			super();
		}			
		
		/**
		 * remove register font because Fonts loaded from other domains can't be regsitered
		 * @param	p_Loading
		 */
		protected override function registerClasses(p_Loading:String):void {
			for each (var swf:XML in __XML.Loadings.Swfs.Swf.(@loading == p_Loading)) {
				var swfid:String = swf.attribute("id");				
				for each (var item:XML in __XML.Loadings.Swfs.Swf.(@id == swfid).Class) {								
					var id:String = item.attribute("id");
					var cl:Class = __LoadersManager.getLoaderById(swfid).getClassById(id);
					if (cl != null) {
						__Classes[id] = cl;						
						if (item.attribute("isFont")=="true") {
							//do nothing
						}
					}
				}
			}			
		}
		/**
		 * won't throw error exception for controler class since we do not need to use them in editor
		 * @param	id
		 * @return
		 */		
		public override function getClassById(id:String):Class {
			var cl:Class = __Classes[id];
			if (cl != null) {
				e = new cl();
			} else {
				c.e('ClassNotRegistered['+id+']; try to instantiate anyway ...');
				try {							
					cl = getDefinitionByName(id) as Class;
					e = new cl();					
				} catch (e:ReferenceError) {
					c.e("ClassNotExist[" + id +"]; using Sprite in editor mode");
					cl = getDefinitionByName("flash.display.Sprite") as Class;
				}
			}
			return cl;
		}
	}
}